package ro.gateway.aida.usr;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ShowUserInfoTag.java,v 1.1 2004/10/24 23:37:05 mihaipostelnicu Exp $
 */
public class ShowUserInfoTag extends TagSupport
{
      private String tableClass;
      private String cellClass;
      private String linkClass;
      private String loginPage;
      private String loginPageLabel;
      private String titleClass;
      private String titleLabel;
      private boolean showLoginLink = false;

      public void setShowLoginLink( boolean showLoginLink )
      {
            this.showLoginLink = showLoginLink;
      }

      public void setTableClass( String tableClass )
      {
            this.tableClass = tableClass;
      }

      public boolean isShowLoginLink()
      {
            return showLoginLink;
      }

      public String getTableClass()
      {
            return tableClass;
      }

      public void setCellClass( String cellClass )
      {
            this.cellClass = cellClass;
      }

      public String getCellClass()
      {
            return cellClass;
      }

      public String getLoginPage()
      {
            return loginPage;
      }

      public String getLinkClass()
      {
            return linkClass;
      }

      public void setLoginPage( String loginPage )
      {
            this.loginPage = loginPage;
      }

      public void setLinkClass( String linkClass )
      {
            this.linkClass = linkClass;
      }

      public void setLoginPageLabel( String loginPageLabel )
      {
            this.loginPageLabel = loginPageLabel;
      }

      public String getLoginPageLabel()
      {
            return loginPageLabel;
      }

      public String getTitleLabel()
      {
            return titleLabel;
      }

      public String getTitleClass()
      {
            return titleClass;
      }

      public void setTitleLabel( String titleLabel )
      {
            this.titleLabel = titleLabel;
      }

      public void setTitleClass( String titleClass )
      {
            this.titleClass = titleClass;
      }

      /**
       * login.jsp nu are root-u ales k lumea
       * :{
       *
       * */
      public int doEndTag() throws JspException
      {
            HttpSession session = pageContext.getSession();
            User user = ( User ) session.getAttribute( UserConstants.LOGGED_USER );
            HttpServletRequest request = ( HttpServletRequest ) pageContext.getRequest();
            HttpServletResponse response = ( HttpServletResponse ) pageContext.getResponse();
            response.setHeader( "Pragma", "no-cache" ); //HTTP 1.0
            response.setDateHeader( "Expires", -1 ); //prevents caching at the proxy server
            response.setHeader( "Cache-Control", "no-store" ); //HTTP 1.1

            StringBuffer sb = new StringBuffer();

            if ( tableClass != null )
            {
                  sb.append( "<table class=\"" ).append( tableClass ).append( "\">" );
            }
            else
            {
                  sb.append( "<table>" );
            }

            if ( titleLabel != null )
            {
                  if ( titleClass == null )
                  {
                        sb.append( "<tr><td>" );
                  }
                  else
                  {
                        sb.append( "<tr><td class=\"" ).append( titleClass ).append( "\">" );
                  }

                  sb.append( titleLabel ).append( "</td></tr>" );
            }

            if ( user != null )
            {
                  if ( cellClass == null )
                  {
                        sb.append( "<tr><td>" );
                  }
                  else
                  {
                        sb.append( "<tr><td class=\"" ).append( cellClass ).append( "\">" );
                  }

                  sb.append( user.getFirstNames() ).append( " " ).append( user.getLastName() );

                  //append( ", " );
                  //append( user.getLogin() );
                  sb.append( "</td></tr>" );
            }

            if ( showLoginLink )
            {
                  if ( cellClass == null )
                  {
                        sb.append( "<tr><td>" );
                  }
                  else
                  {
                        sb.append( "<tr><td class=\"" ).append( tableClass ).append( "\">" );
                  }

                  sb.append( "<a " );

                  if ( linkClass != null )
                  {
                        sb.append( "class=\"" ).append( linkClass ).append( "\" " );
                  }

                  if ( loginPage == null )
                  {
                        sb.append( "href=\"" ).append( request.getContextPath() )
                              .append( "/login.jsp" ).append( "\">" ).append( "Login page" )
                              .append( "</a>" );
                  }
                  else
                  {
                        sb.append( "href=\"" ).append( loginPage ).append( "\">" )
                              .append( loginPageLabel ).append( "</a>" );
                  }

                  sb.append( "</td></tr>" );
            }

            sb.append( "</table>" );

            JspWriter writer = pageContext.getOut();

            try
            {
                  writer.println( sb.toString() );
            }
            catch ( IOException ioEx )
            {
                  System.err.println( ioEx );
            }

            this.tableClass = null;
            this.cellClass = null;
            this.linkClass = null;
            this.loginPage = null;
            this.loginPageLabel = null;
            this.showLoginLink = false;

            return EVAL_PAGE;
      }

      public int doStartTag() throws JspException
      {
            return SKIP_BODY;
      }
}
